
#include "main.h"

#define  HALL_U_PORT    GPIOB
#define  HALL_U_PIN     GPIO_Pin_4

#define  HALL_V_PORT    GPIOB
#define  HALL_V_PIN     GPIO_Pin_5

#define  HALL_W_PORT    GPIOB
#define  HALL_W_PIN     GPIO_Pin_0


unsigned char IS_HALL_U_1(void)      {  return 	GPIO_ReadInputDataBit(HALL_U_PORT,HALL_U_PIN);}
unsigned char IS_HALL_V_1(void)      {  return 	GPIO_ReadInputDataBit(HALL_V_PORT,HALL_V_PIN);}
unsigned char IS_HALL_W_1(void)      {  return 	GPIO_ReadInputDataBit(HALL_W_PORT,HALL_W_PIN);}

//=========================================================
//=========================================================
void Hall_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
	
  {
    GPIO_InitTypeDef  GPIO_InitStructure;
	  
		GPIO_InitStructure.GPIO_Pin = HALL_U_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(HALL_U_PORT, &GPIO_InitStructure);     

	  GPIO_InitStructure.GPIO_Pin = HALL_V_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(HALL_V_PORT, &GPIO_InitStructure);   
	
	  GPIO_InitStructure.GPIO_Pin = HALL_W_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(HALL_W_PORT, &GPIO_InitStructure);   
  }
	
	{
	  EXTI_InitTypeDef EXTI_InitStructure;
		
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource4);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
		
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitStructure.EXTI_Line=EXTI_Line4;
    EXTI_Init(&EXTI_InitStructure);
    EXTI_InitStructure.EXTI_Line=EXTI_Line5;
    EXTI_Init(&EXTI_InitStructure);
    EXTI_InitStructure.EXTI_Line=EXTI_Line0;
    EXTI_Init(&EXTI_InitStructure);
	}
	
	{
 	  NVIC_InitTypeDef NVIC_InitStructure;
	
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;					
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
	  NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
	  NVIC_Init(&NVIC_InitStructure); 
	  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
	  NVIC_Init(&NVIC_InitStructure); 
	  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	  NVIC_Init(&NVIC_InitStructure); 
	}
}

//=========================================================
//=========================================================
unsigned char  get_hall_num(void)
{
  unsigned char i=0;
	
	if(IS_HALL_U_1())
		i |= 0x01;
	if(IS_HALL_V_1())
		i |= 0x02;
	if(IS_HALL_W_1())
		i |= 0x04;
	
	return i;
}

//=========================================================
//=========================================================
void EXTI4_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line4)!=RESET)
  {
	  Change_Phase();
    EXTI_ClearITPendingBit(EXTI_Line4);     /* Clear Flag */  
  }
}

//=========================================================
//=========================================================
void EXTI9_5_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
  {
    Change_Phase();
    EXTI_ClearITPendingBit(EXTI_Line5);     /* Clear Flag */  
  }
}

//=========================================================
//=========================================================
void EXTI0_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
  {
    Change_Phase();
    EXTI_ClearITPendingBit(EXTI_Line0);     /* Clear Flag */  
  }
}
